﻿using AutoMapper;
using MediatR;
using MyMES.Domain.yjb;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.settingCommand;
using MyMES.Read.API.Dtos;
using MyMES.Read.API.FileNlog;

namespace MyMES.Read.API.Application.CommendHandlers.settingCommand
{
    public class ShowCGCommandHandlers : IRequestHandler<ShowCGCommand, APIPaging<List<CDLBDTO>>>
    {
        private readonly IBaseRepository<CDLBDTO> accreop;
        private readonly Nolgs nlogselvse;
        private readonly IMapper mapper;

        public ShowCGCommandHandlers(IBaseRepository<CDLBDTO> accreop, Nolgs nlogselvse)
        {
            this.accreop = accreop;
            this.nlogselvse = nlogselvse;
            this.mapper = mapper;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<CDLBDTO>>> Handle(ShowCGCommand request, CancellationToken cancellationToken)
        {
          
            try
            {
                APIPaging<List<CDLBDTO>> result = new APIPaging<List<CDLBDTO>>();
                result.Code = APIEnum.查询失败;
                result.Msg = "查询成功";
                string sql = "SELECT * FROM PurchaseOrder JOIN PGZJ ON PurchaseOrder.PurchaseOrderId = PGZJ.PurchaseOrderId     JOIN GoodsPurchaseInfo ON   PGZJ.SerialNumber=GoodsPurchaseInfo.SerialNumber  WHERE PurchaseOrder.IsDelete = false ORDER BY PurchaseOrder.PurchaseOrderId DESC";

                var list = await accreop.SelectAsync(sql);

                

                result.TotalCount = list.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.Size);
                list = list.Skip((request.Index - 1) * request.Size).Take(request.Size).ToList();

                result.data = list;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
           
        }
    }
}
